package net.maku.talent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.member.entity.MemberUserEntity;
import net.maku.member.service.MemberUserService;
import net.maku.member.vo.MemberUserVO;
import net.maku.system.entity.SysOrgEntity;
import net.maku.system.service.SysDictDataService;
import net.maku.system.service.SysOrgService;
import net.maku.system.vo.SysDictDataVO;
import net.maku.talent.convert.HonorPersonConvert;
import net.maku.talent.dao.HonorPersonDao;
import net.maku.talent.entity.HonorPersonEntity;
import net.maku.talent.query.HonorPersonQuery;
import net.maku.talent.service.HonorPersonService;
import net.maku.talent.vo.HonorPersonVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 个人荣誉
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-21
 */
@Service
@AllArgsConstructor
public class HonorPersonServiceImpl extends BaseServiceImpl<HonorPersonDao, HonorPersonEntity> implements HonorPersonService {

    private final HonorPersonDao honorPersonDao;
    private final SysDictDataService sysDictDataService;
    private final MemberUserService memberUserService;

    private final SysOrgService sysOrgService;


    @Override
    public PageResult<HonorPersonVO> page(HonorPersonQuery query) {
       //  IPage<HonorPersonEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        IPage<HonorPersonEntity> pageParams = getPage(query);
        QueryWrapper<HonorPersonVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getRealName()),"u.real_name",query.getRealName());
        queryWrapper.eq(query.getGender() != null,"u.gender",query.getGender());
        queryWrapper.eq(StringUtils.isNotBlank(query.getOrgName()),"o.name",query.getOrgName());
        queryWrapper.orderByDesc("nm.id");
        IPage<HonorPersonVO> page = honorPersonDao.selectPageList(pageParams,queryWrapper);
        List<HonorPersonVO> records = page.getRecords();
        setDictInfoData(records);
        return new PageResult<>(records,page.getTotal());
    }

    private void setDictInfoData(List<HonorPersonVO> result) {
        List<SysDictDataVO> honorLevel = sysDictDataService.selectDictDataByType("honor_level");
        List<SysDictDataVO> honorCategory = sysDictDataService.selectDictDataByType("honor_category");
        if(CollectionUtils.isEmpty(result)){
            return;
        }
        result.forEach(r->{
            Integer level = r.getLevel();
            if(CollectionUtils.isEmpty(honorLevel)){
                return;
            }
            honorLevel.forEach(l->{
                String dictValue = l.getDictValue();
                if(String.valueOf(level).equals(dictValue)){
                    r.setLevelStr(l.getDictLabel());
                    return;
                }
            });
            if(CollectionUtils.isEmpty(honorCategory)){
                return;
            }
            Integer category = r.getCategory();
            honorCategory.forEach(t->{
                String dictValue = t.getDictValue();
                if(String.valueOf(category).equals(dictValue)){
                    r.setCategoryStr(t.getDictLabel());
                    return;
                }
            });
        });
    }

    private LambdaQueryWrapper<HonorPersonEntity> getWrapper(HonorPersonQuery query){
        LambdaQueryWrapper<HonorPersonEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(HonorPersonVO vo) {
        HonorPersonEntity entity = HonorPersonConvert.INSTANCE.convert(vo);
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
    }

    @Override
    public void update(HonorPersonVO vo) {
        HonorPersonEntity entity = HonorPersonConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public HonorPersonVO getDetail(Long id) {
        HonorPersonEntity honorPersonEntity = baseMapper.selectById(id);
        HonorPersonVO propagateInnovationStudioVO = BeanUtil.copyProperties(honorPersonEntity, HonorPersonVO.class);
        setDictInfoData(propagateInnovationStudioVO);
        Long memberId = honorPersonEntity.getMemberId();
        MemberUserEntity userEntity = memberUserService.getById(memberId);
        Long orgId = userEntity.getOrgId();
        SysOrgEntity orgEntity = sysOrgService.getById(orgId);
        if(orgEntity != null){
            propagateInnovationStudioVO.setOrgName(orgEntity.getName());
        }
        return propagateInnovationStudioVO;
    }

    private void setDictInfoData(HonorPersonVO result) {
        List<SysDictDataVO> honorLevel = sysDictDataService.selectDictDataByType("honor_level");
        List<SysDictDataVO> honorCategory = sysDictDataService.selectDictDataByType("honor_category");

        Integer level = result.getLevel();
        if(CollectionUtils.isEmpty(honorLevel)){
            return;
        }
        honorLevel.forEach(l->{
            String dictValue = l.getDictValue();
            if(String.valueOf(level).equals(dictValue)){
                result.setLevelStr(l.getDictLabel());
                return;
            }
        });
        if(CollectionUtils.isEmpty(honorCategory)){
            return;
        }
        Integer category = result.getCategory();
        honorCategory.forEach(t->{
            String dictValue = t.getDictValue();
            if(String.valueOf(category).equals(dictValue)){
                result.setCategoryStr(t.getDictLabel());
                return;
            }
        });
    }

}